package com.quyeying.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quyeying.annotation.PassToken;
import com.quyeying.entity.TblProductEntity;
import com.quyeying.entity.TblProductImageEntity;
import com.quyeying.entity.TblProductSkuEntity;
import com.quyeying.service.TblProductService;
import com.quyeying.utils.RedisUtil;
import com.quyeying.utils.httputils.HttpUtil;
import com.quyeying.utils.result.R;
import com.quyeying.utils.result.ResultEnum;
import com.quyeying.utils.result.ReturnPageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
* <p>
* tbl_product 前端控制器
* </p>
* @author jll
* @since 2022-07-21
*/
@Slf4j
@RestController
@RequestMapping("productEntity")
public class TblProductController {

    @Resource
    private TblProductService tblProductService;
    @Resource
    private RedisUtil redisUtil;

    /**
    * 主页商品列表
    **/
    @PassToken
    @GetMapping("/findListByPageClient")
    public R findListByPage(@RequestParam Integer current, @RequestParam Integer size, Long merchantId) {

        try {
            return tblProductService.findListByPageClient(current, size, merchantId);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
    * id查询
    **/
    @PassToken
    @GetMapping("/findById")
    public R findById(Long id, HttpServletRequest httpServletRequest){
        try {
            String authorization = httpServletRequest.getHeader("Authorization");
            long userId;
            if (ObjectUtils.isEmpty(authorization) || ObjectUtils.isEmpty(redisUtil.get(authorization))){
                userId = 0;
            }else {
                userId = Long.parseLong(redisUtil.get(authorization).toString());
            }
            String ipAddress = HttpUtil.getIpAddress(httpServletRequest);
            return tblProductService.findById(id,ipAddress,userId, "");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR.getCode(), e.getMessage());
        }
    }

    /**
     * 搜索接口
     **/
    @PassToken
    @GetMapping("/findListPageSearch")
    public R findListPageSearch(@RequestParam Integer current, @RequestParam Integer size, @RequestParam String search) {

        try {
            return tblProductService.findListPageSearch(current, size, search);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 为我推荐
     **/
    @PassToken
    @GetMapping("/findListPageRecommend")
    public R findListPageRecommend(@RequestParam Integer current, @RequestParam Integer size) {

        try {
            return tblProductService.findListPageRecommend(current, size);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return R.error(ResultEnum.SYSTEM_ERROR);
        }
    }

    /**
     * 特价商品列表
     */
    @PassToken
    @GetMapping("/specialOfferList")
    public R specialOfferList() {
        return tblProductService.specialOfferList();
    }
}
